<?php


namespace app\admin\controller\business;


use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Business extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $business_where[] = ['is_del', '=', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $business_where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['operation_status'])) {
                    $business_where[] = ['operation_status', '=', $filter['operation_status']];
                }
                if (isset($filter['shop_name'])) {
                    $business_where[] = ['shop_name', 'like', '%' . $filter['shop_name'] . '%'];
                }
                if (isset($filter['people'])) {
                    $business_where[] = ['people', 'like', '%' . $filter['people'] . '%'];
                }
                if (isset($filter['phone'])) {
                    $business_where[] = ['phone', 'like', '%' . $filter['phone'] . '%'];
                }
                if (isset($filter['c_address'])) {
                    $business_where[] = ['address', 'like', '%' . $filter['c_address'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $business_where[] = ['province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $business_where[] = ['city', '=', $city['name']];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $business_where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('shop')
                ->where('status', 'in', [1, 2])    //0待审核 3审核失败
                ->page($page, $limit)
                ->where($business_where)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
//                $_list[$k]['c_address'] = $v['province'] . $v['city'] . $v['area'] . $v['address'];
                $_list[$k]['c_address']=$v['address'];
                $member_info = Db::name('member')->find($_list[$k]['member_id']);
                $_list[$k]['nickname'] = $member_info['nickname'];
                $order_num= Db::name('order')->where([
                    ['is_del','=',0],
                    ['shop_id','=',$v['id']],
                    ['status','in',[4,5]],
                ])->count();
                $_list[$k]['order_num']=$order_num;
            }
            //获取数量
            $total = Db::name('shop')
                ->where('status', 'in', [1, 2])    //0待审核 3审核失败
                ->where($business_where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    /**
     * 编辑
     * @return mixed|void
     * todo 图标url map_logo_url
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
           // $address = getLWByAddress($data['row']['province'] . $data['row']['city'] . $data['row']['area'] . $data['row']['address']);
            $address = getLWByAddress( $data['row']['address']);
            $address = json_decode($address, true);
            if ($address['code'] == 0) {
                $data['row']['longitude'] = $address['data'][0];
                $data['row']['latitude'] = $address['data'][1];
            } else {
                $this->error('输入正确位置');
            }
            $business_info = Db::name('shop')
                ->where('is_del', '=', 0)
                ->where('id', $data['id'])
                ->update($data['row']);
            $this->success('操作成功');
        }
        $business_id = $this->request->param('id/d', 0);
        $data = Db::name('shop')->where('is_del', '=', 0)->where('id', $business_id)->find();
        $data['operate_cate'] = explode(',', $data['operate_cate']);

        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $provice = Db::name('area')->where(['name' => $data['province']])->find();

        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();
        $city = Db::name('area')->where(['name' => $data['city']])->find();

        $area_list = Db::name('area')->where([
            'pid' => $city['id']
        ])->select();
        if ($area_list[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $area_list[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $area_list[] = $v;
            }
            unset($area_list[0]);
        }
        usort($area_list, function ($a, $b) {
            return $a['id'] - $b['id'];
        });
        $data_img=Db::name('shop_img')->where([
            'shop_id'=>$business_id
        ])->select();
        $this->assign([
            'data' => $data,
            'city_list' => $city_list,
            'proviec' => $proviec_list,
            'area_list' => $area_list,
            'data_img'=>$data_img
        ]);
        return $this->fetch();
    }

    public function editStatus()
    {
        $business_id = $this->request->param('id/d', 0);
        $data = Db::name('shop')->find($business_id);
        $status = $data['status'] == 2 ? 1 : 2;
        $update = array(
            'id' => $business_id,
            'status' => $status,
        );
        $result = Db::name('shop')->update($update);
        if ($result) {
            return ['code' => 200, 'data' => '', 'msg' => '切换成功'];
        } else {
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
    public function getgoods()
    {
        $order_id = $this->request->param('id/d', 0);
        $data = Db::name('order')->find($order_id);
        $status = $data['is_bu'] == 1 ? 0 : 1;
        $update = array(
            'id' => $order_id,
            'is_bu' => $status,
        );
        $result = Db::name('order')->update($update);
        if ($result) {
            return ['code' => 200, 'data' => '', 'msg' => '切换成功'];
        } else {
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }

    /**
     * 转移账号
     */
    public function change()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
//            $phone_result=validatePhoneNumber($data['phone']);
//            if (!$phone_result){
//                $this->error('输入合法手机号');
//            }
            $member_info = Db::name('member')->where('mobile', $data['phone'])->find();
            if (!$member_info) {
                $this->error('账户不存在');
            }
            $business_info = Db::name('shop')->where('is_del', '=', 0)->where('member_id', $member_info['id'])->find();
            if ($business_info) {
                $this->error('该账户已是商家');
            }
            $update_data = array(
                'id' => $data['id'],
                'member_id' => $member_info['id']
            );
            $result = Db::name('shop')->update($update_data);
            if ($result) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $id = $this->request->param('id/d', 0);
            $this->assign("id", $id);
            return $this->fetch();
        }
    }

    /**
     * 查看
     */
    public function look()
    {
        $id = $this->request->param('id/d', 0);
        //近七天销售额
        //获取进七天时间戳范围
        //前几天 0代表当天的
        $timeScope = getTimestampScope(-7, 0);
        $order2where[] = ['pay_time', 'between', [$timeScope[0], $timeScope[1]]];
        $order1where[] = ['is_tui', '=', 0];
        $order1where[] = ['business_id', '=', $id];
        $day7_amount_count = Db::name('order')
            ->where($order1where)
            ->where($order2where)
            ->sum('price');
        //总销售额
        $amount_count = Db::name('order')
            ->where($order1where)
            ->sum('price');
        //订单总数量
        $order_count = Db::name('order')
            ->where($order1where)
            ->count();
        //提现金额
        $tixian_amount = Db::name('business_finance')
            ->where([
                'business_id' => $id,
                'type' => 2,
                'status' => 1
            ])
            ->sum('true_money');
        $this->assign([
            "business_id" => $id,
            "day7_amount_count" => $day7_amount_count,
            "amount_count" => $amount_count,
            "order_count" => $order_count,
            "tixian_amount" => $tixian_amount
        ]);
        return $this->fetch();
    }

    public function fdetail()
    {
        $param = $this->request->param();
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();

            $where[] = ['status', '=', 1];
            $where[] = ['type', '=', 2];
            $where[] = ['business_id', '=', $param['business_id']];
            $filter = input('filter');

            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['id', '=', $filter['id']];
                }
                if (isset($filter['status_time'])) {
                    $status_time = explode(' - ', $filter['status_time']);

                    foreach ($status_time as $k => $v) {
                        $status_time[$k] = strtotime($v);
                    }
                    $where[] = ['status_time', 'between', [$status_time[0], $status_time[1]]];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }


            $_list = Db::name('business_finance')
                ->where($where)    //'提现状态 0未审核 1审核完成',
                ->page($page, $limit)
                ->order('status_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['status_time'] = date('Y-m-d H:i:s', $v['status_time']);
            }
            $total = Db::name('business_finance')
                ->where($where)    //'提现状态 0未审核 1审核完成',
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'business_id' => $param['business_id'],
            'status' => $param['status'],
        ]);
        return $this->fetch();
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();

        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function del()
    {
        $param = $this->request->param();
        if ($this->request->isAjax()) {
            $update_data = array(
                'id' => $param['id'],
                'is_del' => 1
            );
            $result = Db::name('shop')->update($update_data);
            //删除商品
            Db::name('goods')->where([
                'business_id' => $param['id']
            ])->update([
                'is_del' => 1
            ]);
            if ($result) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        //获取商家信息
        $business_info = Db::name('shop')->find($param['id']);
        //待核销订单
        $dai_count = Db::name('order')->where([
            'business_id' => $param['id'],
            'status' => 2,
            'is_del' => 0
        ])->count();
        //待回款订单 待回款订单金额
        $dai_huikuan = Db::name('order')->where([
            ['business_id', '=', $param['id']],
            ['is_to', '=', 0],
            ['status', 'in', [3, 5, 6]]
        ])->sum('price');
        $this->assign([
            'dai_count' => $dai_count,
            'dai_huikuan' => $dai_huikuan,
            'data' => $business_info,
        ]);
        return $this->fetch();
    }


    public function getc_cate2()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function vorder()
    {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['o.is_del', '=', 0];
            $where[] = ['o.status', 'in', [4,5]];
            $where[] = ['o.shop_id', '=', $data['shop_id']];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['pay_status'])) {
                    $where[] = ['o.pay_status', '=', $filter['pay_status']];
                }

                if (isset($filter['activity_id'])) {
                    $where[] = ['o.activity_id', '=', $filter['activity_id']];
                }

                if (isset($filter['pin_type'])) {
                    $where[] = ['o.pin_type', '=', $filter['pin_type']];
                }

                if (isset($filter['pay_type'])) {
                    $where[] = ['o.pay_type', '=', $filter['pay_type']];
                }

                if (isset($filter['is_bu'])) {
                    $where[] = ['o.is_bu', '=', $filter['is_bu']];
                }

                if (isset($filter['delive_type'])) {
                    $where[] = ['o.delive_type', '=', $filter['delive_type']];
                }

                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['h.province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['h.city', '=', $city['name']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['time'])) {
                    $create_time = explode(' - ', $filter['time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            //获取列表
            $_list = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->order('o.pay_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['pay_time']) {
                    $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
                } else {
                    $_list[$k]['pay_time'] = "------";
                }

                if ($_list[$k]['finish_time']) {
                    $_list[$k]['finish_time'] = date('Y-m-d H:i:s', $v['finish_time']);
                } else {
                    $_list[$k]['finish_time'] = "------";
                }

                $_list[$k]['all_address']=$v['province'].$v['city'].$v['area'].$v['address'];
                $_list[$k]['phone12']=$v['people'].'/'.$v['phone'];

                $order_goods= Db::name('order_goods')->where('order_id','=',$v['id'])->find();
                $_list[$k]['goods_info']=$order_goods['goods_name'].'/'.$order_goods['suk'].'/'.$order_goods['num'];
            }
            //获取数量
            $total = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign([
            'shop_id'=>$data['id'],
            'proviec'=>$proviec,
        ]);
        return $this->fetch();
    }

    public function goods_list(){
        $id = $this->request->param('id/d', 0);
        $goods_list = Db::name('order_goods')
            ->where(['order_id' => $id])
            ->order('sort desc')
            ->select();
        foreach ($goods_list as $k => $v) {
            $goods_info=Db::name('goods')->find($v['goods_id']);
            $goods_list[$k]['goods_info']=$goods_info;
        }
        $this->assign([
            'goods_list' => $goods_list
        ]);
        return $this->fetch('server');
    }
}